<?php

class Auth Extends MY_Controller
{
	/*
		login allowed
		'cashier',
		'inventory',
		'hrd',
		'accounting',
		'engineering',
		'housekeeping',
		'frontoffice',
		'restaurantpos'
	*/
	private $login_for = 'hrd';
	
	public function __construct()
	{
		parent::__construct();
		$this->layout_view = 'login_template';
	}
	
	public function login()
	{
		$this->load->library('token');	
		$this->view_data['form_token'] = $this->token->get_token();
		$this->view_data['system_message'] = $this->_msg();
			
		$this->view_data['login_role'] = strtoupper($this->login_for);
		if($this->input->post('backstage_login'))
		{
			$this->load->library(array('form_validation'));
			
			$this->form_validation->set_rules('user_login','Username','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('user_password','Password','required|trim|htmlspecialchars');
			if($this->token->validate_token($this->input->post('form_token')))
			{
				$this->load->model('M_auth','a');
				
				if($this->form_validation->run() !== FALSE)
				{
					if($this->a->validate_login($this->input->post('user_login'),$this->input->post('user_password'),$this->login_for))
					{
						$this->token->destroy_token();
						$this->_msg('s','Welcome Administrator','home');
					}else{
						$this->token->destroy_token();
						$this->_msg('e','Invalid Login Credentials','auth/login');
					}
				}else{
				$val_errors = validation_errors() == '' ? NULL : $this->_msg('e',validation_errors());
				$this->token->destroy_token();
				$this->_msg('e','Invalid Form Submit',$val_errors);
				}
			}else{
				$this->token->destroy_token();
				$this->_msg('e','Invalid Form Submit','auth/login');
			}
		}
	}
	
	public function logout()
	{
		$this->session->sess_destroy();
		$this->_msg('n','You are Now logged out','auth/login');
	}
}